package app.auto.runner.base.utility;

import java.io.File;
import java.io.FileInputStream;

public class ImageFileUtil {
    public static boolean isImage(String path) {
        return isImagePrivate(path);

    }

    private static boolean isImagePrivate(String path) {
        // File file = new File("C:/Users/Administrator/Desktop/111.png");
        // File file = new File("C:/Users/Administrator/Desktop/111.jpg");
        File file = new File(path);
        if (!file.exists()) {
            System.err.println("--- file not exists! ---");
            return false;
        }

        byte[] png_type = new byte[]{(byte) 0x89, (byte) 0x50, (byte) 0x4E,
                (byte) 0x47, (byte) 0x0D, (byte) 0x0A, (byte) 0x1A,
                (byte) 0x0A, '\0'};
        byte file_head[] = new byte[9];

        try {
            FileInputStream fis = new FileInputStream(file);
            fis.read(file_head, 0, 9);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

        file_head[8] = '\0';
        switch (file_head[0]) {
            case (byte) 0xff:
                if (file_head[1] == (byte) 0xd8)
//                    return "jpg";// jpeg
                    return true;

            case (byte) 0x42:
                if (file_head[1] == (byte) 0x4D)
//                    return "bmp";// bmp
                    return true;

            case (byte) 0x89:
                if (file_head[1] == png_type[1] && file_head[2] == png_type[2]
                        && file_head[3] == png_type[3]
                        && file_head[4] == png_type[4]
                        && file_head[5] == png_type[5]
                        && file_head[6] == png_type[6]
                        && file_head[7] == png_type[7])
//                    return "png";// png
                    return true;
        }

        return false;
    }
}

